package com.order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.db.DB;
import com.vo.Order;
import com.vo.OrderDetail;

public class OrderDao implements IOrderDao {

	// 查询所有订单
	public List<Order> getAll(int page ,int pageCount) throws SQLException {

		DB db = new DB();
		Connection conn = db.getConn();
		String sql = "SELECT eo_id,eo_user_name,eo_user_address,eo_status FROM easybuy_order LIMIT ?,?; ";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, (page-1)*10);
		ps.setInt(2, pageCount);
		ResultSet rs = ps.executeQuery();
		List<Order> list = new ArrayList<Order>();
		while(rs.next()){
			Order order = new Order();
			order.setEo_id(rs.getInt(1));
			order.setEo_user_name(rs.getString(2));
			order.setEo_user_address(rs.getString(3));
			order.setEo_status(rs.getInt(4));
			list.add(order);
		}
		rs.close();
		ps.close();
		db.closeConn();
		return list;
	}

	// 新增订单
	public boolean insert(Order order, List<OrderDetail> list)
			throws SQLException {
		DB db = new DB();
		int oredrId = 0;
		Connection conn = db.getConn();
		// 启动事务
		conn.setAutoCommit(false);
		PreparedStatement ps = null;
		PreparedStatement ps1 = null;
		PreparedStatement ps2 = null;
		String sql = "INSERT INTO easybuy_order (eo_user_id,eo_user_name,eo_user_address,eo_create_time,eo_cost,eo_status,eo_type,eo_tel)VALUES(?,?,?,NOW(),?,?,?,?);";
		ps = conn.prepareStatement(sql);
		ps.setInt(1, order.getEo_user_id());
		ps.setString(2, order.getEo_user_name());
		ps.setString(3, order.getEo_user_address());
		ps.setFloat(4, order.getEo_cost());
		ps.setInt(5, order.getEo_status());
		ps.setInt(6, order.getEo_type());
		ps.setString(7, order.getEo_tel());
		int a = ps.executeUpdate();

		if (a > 0) {

			String sql1 = "SELECT MAX(eo_id) FROM easybuy_order;";
			ps1 = conn.prepareStatement(sql1);
			//
			ResultSet rs = ps1.executeQuery();
			if (rs.next()) {
				oredrId = rs.getInt(1);
				System.out.println(oredrId);
			}
			rs.close();
		}
		int flag = 0;
		OrderDetail detail = new OrderDetail();
		String sql2 = "INSERT INTO easybuy_order_detail (eo_id,ep_id ,eod_quantity,eod_cost)VALUES(?,?,?,?);";
		for (int b = 0; b < list.size(); b++) {
			detail = list.get(b);

			ps2 = conn.prepareStatement(sql2);
			ps2.setInt(1, oredrId);
			ps2.setInt(2, detail.getEp_id());
			ps2.setInt(3, detail.getEod_quantity());
			ps2.setFloat(4, detail.getEod_cost());
			flag = ps2.executeUpdate();
			conn.commit();

		}
		ps2.close();
		ps1.close();
		ps.close();
		conn.close();
		return flag > 0;
	}

	//修改订单
	public int updateOrder(Order order) throws SQLException {
		// TODO Auto-generated method stub
		int flag = 0;
		DB db = new DB();
		Connection conn = db.getConn();
		String sql="UPDATE easybuy_order SET eo_user_address = ?,eo_user_name = ? where eo_id="+order.getEo_id();
		PreparedStatement ps = conn.prepareStatement(sql);
		
		ps.setString(1, order.getEo_user_address());
		ps.setString(2, order.getEo_user_name());
		flag = ps.executeUpdate();
		return flag;
	}
	//
	public int getCount() throws SQLException {
		// TODO Auto-generated method stub
		DB db = new DB();
		int count = 0;
		Connection conn = db.getConn();
		String sql ="SELECT COUNT(*) FROM easybuy_order;";
		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			count = rs.getInt(1);
		}
		rs.close();
		ps.close();
		db.closeConn();
		return count;
	}
	//s删除订单
	public int deleteOrder(int orderId) throws SQLException {
		// TODO Auto-generated method stub
		int flag = 0;
		DB db = new DB();
		Connection conn = db.getConn();
		String sql = "DELETE FROM easybuy_order WHERE eo_id="+orderId;
		PreparedStatement ps = conn.prepareStatement(sql);
		flag = ps.executeUpdate();
		return flag;
	}

	
	
	public static void main(String[] args) {
		/*Order order = new Order();
		order.setEo_cost(700f);
		order.setEo_status(1);
		order.setEo_tel("18374771718");
		order.setEo_user_name("tt");
		order.setEo_user_address("衡阳");
		order.setEo_type(1);
		order.setEo_user_id(10);

		List<OrderDetail> list = new ArrayList<OrderDetail>();
		OrderDetail detail = new OrderDetail();
		detail.setEod_cost(order.getEo_cost());
		detail.setEod_quantity(3);
		detail.setEp_id(1);

		OrderDetail detail1 = new OrderDetail();
		detail1.setEod_cost(1000f);
		detail1.setEod_quantity(8);
		detail1.setEp_id(2);
		list.add(detail);
		list.add(detail1);
		OrderDao dao = new OrderDao();
		/*
		 * try { boolean flag = dao.insert(order, list);
		 * System.out.println(flag); } catch (SQLException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); }
		 */
		
		/*List<Order> list = new ArrayList<Order>();
		OrderDao dao = new OrderDao();
		int page = 1,pageCount =10;
		try {
			list = dao.getAll(page,pageCount);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		//测试修改
		/*OrderDao dao = new OrderDao();
		Order order = new Order();
		order.setEo_id(18);
		order.setEo_user_address("西班牙");
		order.setEo_user_name("DYL");
		try {
			int flag = dao.updateOrder(order);
			System.out.println(flag);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		/*OrderDao dao = new OrderDao();
		int id = 39;
		try {
			System.out.println(dao.deleteOrder(id));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}

	
	

	

}
